using System;
using Coderoom.ScrumPlanner.Domain;
using Coderoom.ScrumPlanner.Tests.FakeRepositories;
using Xunit;

namespace Coderoom.ScrumPlanner.Tests.Unit.FakeRepositoryTests
{
	public class FakeSprintRepositoryTests
	{
		private readonly FakeSprintRepository _repository;

		public FakeSprintRepositoryTests()
		{
			_repository = new FakeSprintRepository();
		}

		[Fact]
		public void GetSprintsByProjectId_returns_SprintCollection()
		{
			// arrange
			var projectId = new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6");

			// act
			var sprints = _repository.GetSprints(projectId);

			// assert
			Assert.NotNull(sprints);
			Assert.IsType<SprintCollection>(sprints);
			Assert.Equal(2, sprints.Count);

			Assert.Equal(new Guid("253E448B-FE44-4E03-82A2-10BBBE9FC162"), sprints[0].Id);
			Assert.Equal("Sprint 0", sprints[0].Title);
			Assert.Equal("sprint-0", sprints[0].Slug);

			Assert.Equal(new Guid("01613EB9-85F8-4241-8AE6-B6F2F6168C4A"), sprints[1].Id);
			Assert.Equal("Sprint 1", sprints[1].Title);
			Assert.Equal("sprint-1", sprints[1].Slug);
		}

		[Fact]
		public void GetSprint_returns_Sprint_when_sprint_is_found()
		{
			// arrange
			var id = new Guid("01613EB9-85F8-4241-8AE6-B6F2F6168C4A");

			// act
			var sprint = _repository.GetSprint(id);

			// assert
			Assert.NotNull(sprint);
			Assert.IsType<Sprint>(sprint);
			Assert.Equal(id, sprint.Id);
			Assert.Equal("Sprint 1", sprint.Title);
			Assert.Equal("sprint-1", sprint.Slug);
		}

		[Fact]
		public void GetSprint_returns_null_when_sprint_is_not_found()
		{
			// arrange
			var id = Guid.Empty;

			// act
			var sprint = _repository.GetSprint(id);

			// assert
			Assert.Null(sprint);
		}

		[Fact]
		public void CreateSprint_returns_void_when_sprint_is_created()
		{
			// arrange
			var projectId = new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6");
			var sprints = _repository.GetSprints(projectId);
			Assert.Equal(2, sprints.Count);

			var newSprint = new Sprint();
			newSprint.Title = "Sprint 2";
			newSprint.Slug = "sprint-2";
			newSprint.ProjectId = projectId;

			// act
			_repository.CreateSprint(newSprint);

			// assert
			sprints = _repository.GetSprints(projectId);

			Assert.Equal(3, sprints.Count);

			Assert.NotEqual(Guid.Empty, sprints[2].Id);
			Assert.Equal("Sprint 2", sprints[2].Title);
			Assert.Equal("sprint-2", sprints[2].Slug);
			Assert.Equal(projectId, sprints[2].ProjectId);
		}

		[Fact]
		public void DeleteSprint_returns_true_when_sprint_is_deleted()
		{
			// arrange
			var projectId = new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6");

			Assert.Equal(2, _repository.GetSprints(projectId).Count);

			var id = _repository.GetSprints(projectId)[1].Id;

			// act
			var result = _repository.DeleteSprint(id);

			// assert
			Assert.True(result);
			Assert.Equal(1, _repository.GetSprints(projectId).Count);
		}

		[Fact]
		public void DeleteSprint_returns_false_when_sprint_is_not_deleted()
		{
			// arrange
			var projectId = new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6");

			Assert.Equal(2, _repository.GetSprints(projectId).Count);

			var id = Guid.Empty;

			// act
			var result = _repository.DeleteSprint(id);

			// assert
			Assert.False(result);
			Assert.Equal(2, _repository.GetSprints(projectId).Count);
		}
	}
}